/*************************************************************************
	> File Name: show_bmp.c
	>Author :卢孙远(Simon)
	> company: GDOU
	> Mail: lsy476941913@live.com
	> Created Time: 2017年03月5日 星期四 20时36分17秒
 ************************************************************************/
#include "show_bmp.h"
int width, height;
/*
 *	@Function:将24位的位图格式转化为32（uint32_t）位指针
 *	@Param:width:图宽度,height:位图高度,dst:32位目的地址,src:24位位图原地址
 *	@Author:卢孙远(Simon)
 */
int
bitmap_format_convert(int width, int height, uint32_t * dst, char *src)
{
	int i = 0;
	int x = 0;
	int y = 0;
	for (y = 0, i = 0; y < height; y++)
		for (x = 0; x < width; x++, i += 3)
			dst[(height - 1 - y) * width + x] =
			src[i] << 0 | src[i + 1] << 8 | src[i + 2] << 16;
	return 0;
}

/*
 *	@Function:画点
 *	@Param:fb_mem:fb的映射内存地址,x:像素点的x坐标,y:像素点的y坐标,color:32位颜色
 *	@Author:卢孙远(Simon)
 */
void
draw_point(char *fb_mem, int x, int y, uint32_t color)
{
	memcpy(fb_mem + (y * 800 + x) * 4, &color, sizeof(uint32_t));
}

/*
 *	@Function:画矩形
 *	@Param:fb_mem:fb的映射内存地址, x:像素点的x坐标, y:像素点的y坐标, width图片宽度 height：图片高度， bmp_src:32位图地址
 *	@Author:卢孙远(Simon)
 */
void
draw_rect_fill(char *fb_mem, int x, int y, int width, int height,
	uint32_t * bmp_src)
{
	int i = 0, j = 0;
	for (j = y; j < y + height; j++)
		for (i = x; i < x + width; i++) {
			draw_point(fb_mem, i, j, *bmp_src);
			bmp_src += 1;
		}
}

char*
get_bmp(char *path)
{
	FILE *fp;
	char *bmp_buf = NULL;
	char *buf = NULL;
	int flen = 0;
	int ret = -1;
	int total_length = 0;

	if (path == NULL) {
		printf("path Error,return\n");
		return NULL;
	}

	fp = fopen(path, "rb");
	if (fp == NULL) {
		printf("load cursor file open failed\n");
		return NULL;
	}
	/* 求解文件长度 */
	fseek(fp, 0, SEEK_SET);
	fseek(fp, 0, SEEK_END);
	flen = ftell(fp);

	/* 再移位到文件头部 */
	fseek(fp, 0, SEEK_SET);
	ret = fread(&FileHead, sizeof(BITMAPFILEHEADER), 1, fp);
	if (ret != 1) {
		printf("read header error!\n");
		fclose(fp);
		return(NULL);
	}
	//检测是否是bmp图像
	if (memcmp(FileHead.cfType, "BM", 2) != 0) {
		printf("it's not a BMP file\n");
		fclose(fp);
		return(NULL);
	}
	ret = fread((char *) &InfoHead, sizeof(BITMAPINFOHEADER), 1, fp);
	if (ret != 1) {
		printf("read infoheader error!\n");
		fclose(fp);
		return(NULL);
	}
	width = InfoHead.ciWidth;
	height = InfoHead.ciHeight;
	bmp_buf = (char *) malloc(sizeof(char) * ((width * height * 3)));
	if (bmp_buf == NULL) {
		printf("load > malloc bmp out of memory!\n");
		return NULL;
	}

	total_length = width * height * 3;

	//跳转到数据区
	fseek(fp, FileHead.cfoffBits, SEEK_SET);

	//每行字节数ttt
	buf = bmp_buf;
	int padding = (4 - (width * 3) % 4) % 4;
	int line_size = width * 3;
	while ((ret = fread(buf, 1, line_size, fp)) >= 0) {
		if (ret == line_size) {
			fseek(fp, padding, SEEK_CUR);
		}
		if (ret == 0) {
			usleep(100);
			continue;
		}
		buf += ret;
		total_length = total_length - line_size;
		if (total_length == 0) {
			break;
		}
	}
	///重新计算，很重要！！
	//total_length = width * height;
	//关闭图片
	fclose(fp);
	return bmp_buf;
}

int
show_bmp(char *fb_mem, char *path, int x, int y)
{
	//显示图像
	char *bmp_buf = get_bmp(path);
	uint32_t *bmp_buf_dst = NULL;

	bitmap_format_convert(width, height, bmp_buf_dst, bmp_buf);
	draw_rect_fill(fb_mem, x, y, width, height, bmp_buf_dst);

	
	free(bmp_buf);
	free(bmp_buf_dst);
	return 0;
}

